package com.mnemonic.views;

import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import com.mnemonic.game.Game;
import com.mnemonic.level.Level;
import com.mnemonic.lifecycle.Lifecycles;
import com.mnemonic.player.Player;
import com.mnemonic.storage.impl.LevelStorageService;
import com.mnemonic.storage.impl.PlayerStorageService;
import com.mnemonic.support.swing.Dialogs;

/**
 * 对战引导页
 * 
 * @author lifeng
 * @date 2021/05/12
 */
public class PlayPanel extends AbstractPanel {

    private static final long serialVersionUID = 1L;

    private LevelList levels;
    private PlayerList players;

    public PlayPanel() {
        this.setLayout(null);

        // 选择难度
        JLabel label1 = new JLabel("选择难度（选择一个难度）：");
        label1.setBounds(0, 0, 500, 20);
        this.add(label1);

        levels = new LevelList();
        JScrollPane ps = new JScrollPane(levels);
        ps.setBounds(0, 30, 500, 80);
        this.add(ps);

        // 选择玩家
        JLabel label2 = new JLabel("选择玩家（按住 ctrl + 左键 ，至少选择两个玩家）：");
        label2.setBounds(0, 160, 500, 20);
        this.add(label2);
        players = new PlayerList();
        ps = new JScrollPane(players);
        ps.setBounds(0, 190, 500, 150);
        this.add(ps);

        // 开始
        JButton play = new JButton("开战");
        play.addActionListener((e) -> {
            this.doPlay();
        });
        play.setBounds(0, 350, 100, 40);
        this.add(play);

        // 刷新
        JButton refresh = new JButton("刷新");
        refresh.setBounds(110, 350, 100, 40);
        this.add(refresh);
    }

    @Override
    public void init() {
        if (!(this.state == null || this.state == Lifecycles.Start)) {
            return;
        }
        super.init();
        levels.init();
        players.init();
    }

    /**
     * 开始
     */
    public void doPlay() {

        // 选择的难度
        Level level = this.levels.getSelectLevel();

        // 选择的玩家
        List<Player> players = this.players.getSelectPlayers();

        // 验证
        if (level == null || players == null || players.size() < 2) {
            Dialogs.error("错误提醒", "请选择难度且至少选择两名玩家");
            return;
        }
        this.doGame(level, players);
    }

    private void doGame(Level level, List<Player> players) {
        new GamePage(Game.newGame(level, players)).setVisible(true);
    }

    /**
     * 难度
     * 
     * @author lifeng
     * @date 2021/05/13
     */
    public class LevelList extends JList<Level> {

        private static final long serialVersionUID = 1L;
        private DefaultListModel<Level> model;
        private List<Level> levels;
        private LevelStorageService levelStorageService;

        public LevelList() {
            model = new DefaultListModel<>();
            this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            this.setSelectedIndex(0);
            this.setModel(model);
        }

        public void init() {
            this.levelStorageService = new LevelStorageService();
            this.levels = this.levelStorageService.list();
            for (Level level : this.levels) {
                this.model.addElement(level);
            }
        }

        public Level getSelectLevel() {
            int index = this.getSelectedIndex();
            if (index == -1) {
                return null;
            }
            return this.levels.get(index);
        }
    }

    /**
     * 玩家
     * 
     * @author lifeng
     * @date 2021/05/13
     */
    public class PlayerList extends JList<Player> {

        private static final long serialVersionUID = 1L;
        private DefaultListModel<Player> model;
        private List<Player> players;
        private PlayerStorageService playerStorageService;

        public PlayerList() {
            model = new DefaultListModel<>();
            this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            this.setSelectedIndex(0);
            this.setModel(model);
        }

        public void init() {
            this.playerStorageService = new PlayerStorageService();
            this.players = this.playerStorageService.list();
            for (Player level : this.players) {
                this.model.addElement(level);
            }
        }

        public List<Player> getSelectPlayers() {
            int[] indexs = this.getSelectedIndices();
            if (indexs == null || indexs.length < 2) {
                return null;
            }
            List<Player> sels = new ArrayList<>();
            for (int i : indexs) {
                sels.add(this.players.get(i));
            }
            return sels;
        }
    }
}
